#include "../inc/inc.h"
#include "../inc/cmd.h"
#include "../inc/log.h"


int cmdCd(char *cmdBuffSeg[], int cmdSegNum)
{
    logWrite("cmd cd");
    return 0;
}

int cmdCp(char *cmdBuffSeg[], int cmdSegNum)
{
    logWrite("cmd cp");
    return 0;
}

int cmdQuit(char *cmdBuffSeg[], int cmdSegNum)
{
    logWrite("cmd quit");
    exit(0);
    return 0;
}

const CmdTypedef g_Cmd[] = 
{
    { "cd"   , cmdCd   },
    { "cp"   , cmdCp   },
    { "quit" , cmdQuit }, 
};


int cmdHandle(char cmdBuff[])
{
    char *cmdBuffSeg[MAX_CMD_STR_SEG_NUM] = {0};

    cmdBuffSeg[0] = strtok(cmdBuff, " ");
    int iCmdSegNum;
    for(iCmdSegNum=1; iCmdSegNum<MAX_CMD_STR_SEG_NUM; ++iCmdSegNum)
    {
        if ( ( cmdBuffSeg[iCmdSegNum] = strtok(NULL, " ") ) == NULL )
        {
            break;
        }
    } 
    
    int i;
    int iTotalCmdNum = sizeof(g_Cmd)/sizeof(g_Cmd[0]);
    for(i=0;i<iTotalCmdNum; ++i)
    {
        if( !strcmp( cmdBuffSeg[0], g_Cmd[i].CmdName) )
        {
            g_Cmd[i].CmdFun(cmdBuffSeg, iCmdSegNum);
            return 0;
        }
    }
       
    logWrite("Invalid cmd");
    printf("Invalid cmd\n");
    return -1;
}

int cmdUsrTips(void)
{
    char strCwd[200] = {0};
    if ( NULL == getcwd(strCwd, sizeof(strCwd)) )
    {
        logWrite("Fail to getcwd in cumdUsrTips");
        perror("Fail to getcwd in cumdUsrTips");
        return -1;
    }

    printf("%s$ ", strCwd); 
    return 0;
}

